import { _decorator, Component, error, JsonAsset, Label} from 'cc';
import { EventManager } from './manager/EventManager';
import { EventEnum } from './enum/EventEnum';
import Mgr from './manager/Mgr';
import { ConfigManager } from './manager/ConfigManager';
const { ccclass, property } = _decorator;

@ccclass('LoadingView')
export class LoadingView extends Component {
    private _txtProgress:Label;
    private _commonResReady:boolean = false;
    private _progressNum:number = 0;
    start() {
        let self = this;
        this._txtProgress = this.node.getChildByName("txtProgress").getComponent(Label);
        console.log("LoadingView start");
        this.scheduleOnce(()=>{
            Mgr.loader.LoadBundle("ui",{
                onProgress:function(e){
                    let progress = self._progressNum;
                    if(e.progress != null && e.progress < 100){
                        progress = self._progressNum + e.progress * 0.5
                        self._txtProgress.string = "资源加载中..." + Math.floor(progress) + "%";
                    }
                    console.log("LoadingView LoadBundle：" + e.progress + " num:" + progress);
                },
                onComplete:function(err,bundle){
                    if(err){
                        console.log("ui bundle加载失败",err);
                        self._txtProgress.string = "ui资源加载失败，请刷新重试...";
                    }
                    else{
                        console.log("ui bundle加载成功",bundle);
                        self._commonResReady = true;
                        self.checkAllReady();
                    }
                }
            });
        },1)
    }

    private checkAllReady(){
        if(this._commonResReady){
            EventManager.dispatch(EventEnum.OnGameResLoadComplete);
            this.node.active = false;
        }
    }

    update(deltaTime: number) {
        
    }
}


